package controller.library
{
	import events.library.EventAddResourcesToLibrary;
	
	import flashx.textLayout.events.ModelChange;
	
	import model.ModelLibrary;
	import model.ModelLibraryConflicts;
	import model.ModelProject;
	
	import org.flashIso.engine.library.objects.LBinaryData;
	import org.flashIso.engine.library.objects.LImage;
	import org.robotlegs.mvcs.Command;
	
	import services.IServiceFile;
	
	public class CommandAddResourcesToLibrary extends Command
	{
		[Inject]
		public var ml:ModelLibrary;
		
		[Inject]
		public var mlc:ModelLibraryConflicts;
		
		[Inject]
		public var e:EventAddResourcesToLibrary;
		
		[Inject]
		public var sb:IServiceFile;
		
		[Inject]
		public var mp:ModelProject;
		
		override public function execute() : void {
			var i:uint;
			if (e.imagesPath != null){
				for (i = 0; i < e.imagesPath.length; i++) {
					var img:LImage = new LImage();
					img.data = sb.readFile(e.imagesPath[i]);
					img.id = sb.getFileName(e.imagesPath[i]);
					if (ml.getLImageByID(img.id) == null){
						ml.addLImage(img);
					}else{
						mlc.conflicImages.addItem(img);
					}
				}
				mp.projectModified = true;
			}
			if (e.binariesPath != null){
				for (i = 0; i < e.binariesPath.length; i++) {
					var b:LBinaryData = new LBinaryData();
					b.data = sb.readFile(e.binariesPath[i]);
					b.id = sb.getFileName(e.binariesPath[i]);
					if (ml.getLBinaryDataByID(b.id) == null){
						ml.addLBinaryData(b);
					}else{
						mlc.conflictBinaries.addItem(b);
					}
				}
				mp.projectModified = true;
			}
		}
	}
}